package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.operate.dto.SeckillAddsDto;
import com.wx.mall.operate.entity.SeckillAdds;
import com.wx.mall.operate.entity.SeckillItem;
import com.wx.mall.operate.mapper.SeckillAddsMapper;
import com.wx.mall.operate.service.ISeckillAddsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.operate.service.ISeckillItemService;
import com.wx.mall.operate.vo.SeckillAddToItemVo;
import com.wx.mall.operate.vo.SeckillAddsReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-28
 */
@Service
public class SeckillAddsServiceImpl extends ServiceImpl<SeckillAddsMapper, SeckillAdds> implements ISeckillAddsService {

    @Autowired
    ISeckillItemService iSeckillItemService;

    @Override
    public List<SeckillAdds> findSecAddsList(SeckillAddsDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<SeckillAdds> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("goods_money").eq("difference_type",1);
        List<SeckillAdds> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SeckillAdds> findSecAddsTwoList(SeckillAddsDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<SeckillAdds> queryWrapper=new QueryWrapper<>();
        if (dto.getGoodsName()!=null){
            queryWrapper.like("goods_name",dto.getGoodsName()).eq("difference_type",1).orderByAsc("goods_money");
        };
        List<SeckillAdds> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public int addsToItem(SeckillAddsReqVo ids) {
        Random random=new Random();
        int a=random.nextInt(100)+1;
        int rac;
        List<SeckillAddToItemVo> list=new ArrayList<>();
        for (Long id:ids.getIds()
        ) {
            SeckillAddToItemVo all=new SeckillAddToItemVo();
            SeckillAdds byId = this.getById(id);
            SeckillItem seckillItem=new SeckillItem();
            seckillItem.setNumber(byId.getId());
            seckillItem.setSkuId(byId.getSkuId());
            seckillItem.setSpuId(byId.getSpuId());
            seckillItem.setName(byId.getGoodsName());
            seckillItem.setSpuNum(byId.getGoodsNum());
            seckillItem.setMoney(byId.getGoodsMoney());
            seckillItem.setSurplus(a);
            seckillItem.setDeletes(1);
            byId.setDifferenceType(0);
            boolean save = iSeckillItemService.save(seckillItem);
            boolean b = this.updateById(byId);
            if (save==true || b==true){
                SeckillItem byId1 = iSeckillItemService.getById(byId.getSecitemId());
                SeckillAdds byId2 = this.getById(id);
                all.setSeckillAdds(byId2);
                all.setSeckillItem(byId1);
            };
            list.add(all);
            return save || b?1:0;
        }
        if (list!=null){
            rac=1;
        }else {
            rac=0;
        }
        return rac;
    }
}
